/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * NOTE:
 * Derivative work based on the Google IO 2011 Schedule app released for open source
 * http://code.google.com/p/iosched/
 */

package edu.bsu.android.gtv.apps.bconnected.util;

import edu.bsu.android.gtv.apps.bconnected.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.text.SpannableString;
import android.text.util.Linkify;

/**
 * A helper for showing the disclaimer to the student and storing a {@link SharedPreferences} bit indicating whether the
 * user has accepted.
 */
public class DisclaimerHelper {
    public static boolean hasAcceptedDisclaimer(final Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getBoolean("accepted_disclaimer", false);
    }
    
    public static boolean disclaimerExists(final Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        // If the student has not logged in and accepted the disclaimer, don't show it on the initial launch of the app
        return sp.contains("accepted_disclaimer");
    }

    public static void setAcceptedDisclaimer(final Context context, final boolean accepted) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {
                SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
                sp.edit().putBoolean("accepted_disclaimer", accepted).commit();
                return null;
            }
        }.execute();
    }

    /**
     * Show various disclaimers to the student.
     *
     * @param accepted True IFF user has accepted license already, which means it can be dismissed.
     *                 If the user hasn't accepted, then the disclaimer must be accepted or the program
     *                 exits.
     * @param activity Activity started from.
     */
    public static void showDisclaimer(final boolean accepted, final Activity activity) {
    	final SpannableString s = new SpannableString(activity.getString(R.string.dialog_disclaimer_text));
    	Linkify.addLinks(s, Linkify.ALL);
    	
        AlertDialog.Builder disclaimer = new AlertDialog.Builder(activity)
                .setTitle(R.string.dialog_disclaimer_title)
                .setIcon(R.drawable.ic_dialog_alert)
                .setMessage(s)
                .setCancelable(accepted);

        if (accepted) {
            // If they've accepted the EULA allow, show an OK to dismiss.
        	disclaimer.setPositiveButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
        } else {
            // If they haven't accepted the EULA allow, show accept/decline buttons and exit on decline.
        	disclaimer
                    .setPositiveButton(R.string.dialog_accept,
                            new android.content.DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    setAcceptedDisclaimer(activity, true);
                                    dialog.dismiss();
                                }
                            })
                    .setNegativeButton(R.string.dialog_decline,
                            new android.content.DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                    activity.finish();
                                }
                            });
        }
        disclaimer.show();
    }
}
